package com.wengu.mythread;

import java.util.concurrent.*;

/**
 * @author wanghailei
 * @date 2023/02/27
 */
public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        System.out.println(System.currentTimeMillis()+"--This is MyCallable.........................begin");
        int sum = 0;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        for (int i = 0; i < 1000; i++) {
            sum += i;
        }
        System.out.println(System.currentTimeMillis()+"--This is MyCallable...................calculate result "+sum);


        return sum;
    }


    public static void main(String[] args) {
        System.out.println(System.currentTimeMillis()+"--This is main.........................begin");
        ThreadPoolExecutor executor = new ThreadPoolExecutor(2,5,60, TimeUnit.SECONDS,new LinkedBlockingQueue<>(8));
        MyCallable myCallable = new MyCallable();
        Future<Integer> future = executor.submit(myCallable);
        executor.shutdown();
        Integer sum = 0;
        System.out.println(System.currentTimeMillis()+"--This is main....................calculating");
        try {
            sum = future.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        System.out.println(System.currentTimeMillis()+"--This is main.........................end");
//        System.out.println(System.currentTimeMillis()+"--This is main....................cal result "+sum);

    }
}
